//
//  BaseClass.m
//
//  Created by   on 2018/8/21
//  Copyright (c) 2018 __MyCompanyName__. All rights reserved.
//

#import "BaseClass.h"


NSString *const kBaseClassReturnCode = @"returnCode";
NSString *const kBaseClassReturnMsg = @"returnMsg";


@interface BaseClass ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation BaseClass

@synthesize returnCode = _returnCode;
@synthesize returnMsg = _returnMsg;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.returnCode = [[self objectOrNilForKey:kBaseClassReturnCode fromDictionary:dict] doubleValue];
            self.returnMsg = [self objectOrNilForKey:kBaseClassReturnMsg fromDictionary:dict];
    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.returnCode] forKey:kBaseClassReturnCode];
    [mutableDict setValue:self.returnMsg forKey:kBaseClassReturnMsg];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    self.returnCode = [aDecoder decodeDoubleForKey:kBaseClassReturnCode];
    self.returnMsg = [aDecoder decodeObjectForKey:kBaseClassReturnMsg];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeDouble:_returnCode forKey:kBaseClassReturnCode];
    [aCoder encodeObject:_returnMsg forKey:kBaseClassReturnMsg];
}

- (id)copyWithZone:(NSZone *)zone {
    BaseClass *copy = [[BaseClass alloc] init];
    
    if (copy) {
        copy.returnCode = self.returnCode;
        copy.returnMsg = [self.returnMsg copyWithZone:zone];
    }
    
    return copy;
}


@end
